﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.IO;
using System.Drawing;

namespace Pixentration
{
	/// <summary>
	/// Interaction logic for Card.xaml
	/// </summary>
	public partial class Card : UserControl
	{

        
        public int cardNumber;
        public delegate void myDelegate(Object sender);
        public event myDelegate showOver;

        /// <summary>
        /// component initialization
        /// </summary>
		public Card()
		{
			this.InitializeComponent();
            
		}
       
        /// <summary>
        /// setter method to set the cardface
        /// </summary>
        /// <remarks> <para>this is only used when the game is initialized as the image
        /// has to be converted over from System.Drawing.Image.  Other getters and 
        /// setters are used mid game</para>
        /// </remarks>
        public System.Drawing.Image cardFace
        {
            set
            {
                
                System.Drawing.Bitmap dImg = (Bitmap)value;
                MemoryStream ms = new MemoryStream();
                dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
                bImg.BeginInit();
                bImg.StreamSource = new MemoryStream(ms.ToArray());
                bImg.EndInit();
                //img is an Image control.
                imgFront.Source = bImg;
 
            }
            get
            {
                return null;
            }

        }

        /// <summary>
        /// setter method for the card back
        /// </summary>
        /// <remarks> <para>Again only used at initilization of game </para>></remarks>
        public System.Drawing.Image cardBack
        {
            set
            {

                System.Drawing.Bitmap dImg = (Bitmap)value;
                MemoryStream ms = new MemoryStream();
                dImg.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
                bImg.BeginInit();
                bImg.StreamSource = new MemoryStream(ms.ToArray());
                bImg.EndInit();
                //img is an Image control.
                imgBack.Source = bImg;

            }
            get
            {
                return null;
            }

        }
        
        /// <summary>
        /// Storyboard to trigger the card's reverse animation
        /// </summary>
        /// <remarks><para> The animations are handled by one of two storyboards
        /// this is the second one to display the card flipping back over</para></remarks>
        public void reverse() 
        {
            Storyboard temp = (Storyboard)TryFindResource("Storyboard2");
            temp.Begin();
        }
        
        /// <summary>
        /// trigger event that fires to notify that the storyboard is finished
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <remarks> <para>This is the event we use to tell the game to test if there are two cards flipped and 
        /// if so check for a match.  We use the end of the animation as the event instead of a click event 
        /// so that the card has a chance to finish its animation before being removed</para></remarks>
        private void Storyboard_Completed(object sender, EventArgs e)
        {
            showOver(this);
        }

        /// <summary>
        /// gets the cardface as an ImageSource image
        /// </summary>
        /// <returns>System.Windows.media.ImageSource image file</returns>
        /// <remarks> <para>We get the card image separately here so there is no need to convert
        /// from one image type to another.  This is used when we switch card faces</para>></remarks>
        public System.Windows.Media.ImageSource getCardFace()
        {
            return imgFront.Source;
        }

        /// <summary>
        /// Alternate setter method that sets the cardface as an imagesource image
        /// </summary>
        /// <param name="anImage">The Card image to be set</param>
        /// <remarks> <para>again this is done separate from the initial set method to save 
        /// an unneccessary conversion and needless overhead </para></remarks>
        public void setCardFace(System.Windows.Media.ImageSource anImage)
        {
            imgFront.Source = anImage;
        }

        /// <summary>
        /// Reset the card image
        /// </summary>
        /// <remarks><para> This is used when starting a new game after the initial one.  
        /// We start the animation for the flip over back to the beginning but then 
        /// cause it to automatically finish so there is no ugly card flip as the
        /// game starts</para></remarks>
        public void reload()
        {
            Storyboard temp = (Storyboard)TryFindResource("Storyboard2");
            temp.Begin();
            temp.SkipToFill();
        }
	}
}